package com.netty.pro.netty.nio;

import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SocketChannel;

/**
 * @author xuzn
 * @ClassName: NioClient
 * @ProjectName echo-project
 * @date 2020/3/8/10:29
 */
public class NioClient {
  public static void main(String[] args) throws Exception {
    SocketChannel socketChannel = SocketChannel.open();
    socketChannel.configureBlocking(false);
    InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 7000);
    boolean connect = socketChannel.connect(inetSocketAddress);
    if (!connect) {
      while (!socketChannel.finishConnect()) {
        System.out.println("~~~~~~~~~~~");
      }
    }
    // 链接成功 发送信息
    String string = "hello nio client";
    // wrap方法不用设置buffer的大小,可以直接和输入的字符串大小一致
    ByteBuffer byteBuffer = ByteBuffer.wrap(string.getBytes());
    socketChannel.write(byteBuffer);
    System.in.read();
  }
}
